const api = require('../../utils/api')
var get = require('../../utils/getRequest');
var post = require('../../utils/postRequest');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    indexLog: api.indexLog,
    indexOfficial: api.indexOfficial,
    shopLog: api.shopLog,
    showPrivacy: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var deviceSn = options.deviceSn;
    if (deviceSn != null && deviceSn != undefined) {
      var type = deviceSn.substr(0, 1);
      if (deviceSn == "SN00000001") {
        wx.navigateTo({
          url: '../b4G/b4G?deviceSn=' + deviceSn
        })
      }
      if (type == "1") { //4G版
        wx.navigateTo({
          url: '../b4G/b4G?deviceSn=' + deviceSn
        })
      } else {
        wx.navigateTo({
          url: '../bluetooth/bluetooth?deviceSn=' + deviceSn
        })
      }
    }
    console.log("onLoad:", options);
    if (api.openId == null) {
      wx.login({
        success: (res) => {
          var code = res.code;
          var data = {
            code: code
          };
          get.getRequest(null, api.getOpenIdByCode, data).then(res => {
            console.log("res.code:", res.code)
            console.log("res.data.openId:", res.data.openId)
            if (res.code == 200) {
              api.openId = res.data.openId;
            }
          })
        },
      })
    }
  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    if (api.openId == null) {
      wx.login({
        success: (res) => {
          var code = res.code;
          var data = {
            code: code
          };
          get.getRequest(null, api.getOpenIdByCode, data).then(res => {
            console.log("res.code:", res.code)
            console.log("res.data.openId:", res.data.openId)
            if (res.code == 200) {
              api.openId = res.data.openId;
            }
          })
        },
      })
    }
    wx.hideLoading();
    this.initPrivacy();
  },
  goToGoods() {
    wx.scanCode({
      onlyFromCamera: true,
      success: function (res) {
        ///pages/index/index?deviceSn=13889878
        var path = res.path;
        try {
          var deviceSn = path.split("?")[1].split("=")[1];
          var type = deviceSn.substr(0, 1);
          if (deviceSn == "SN00000001") {
            wx.navigateTo({
              url: '../b4G/b4G?deviceSn=' + deviceSn
            })
          }
          if (type == "1") { //4G版
            wx.navigateTo({
              url: '../b4G/b4G?deviceSn=' + deviceSn
            })
            return
          }
          if (type == 4) {
            wx.navigateTo({
              url: '../bluetooth8/bluetooth8?deviceSn=' + deviceSn
            })
          } else {
            wx.navigateTo({
              url: '../bluetooth/bluetooth?deviceSn=' + deviceSn
            })
          }
        } catch (e) {
          wx.showToast({
            title: '不是合法的设备二维码'
          })
        }
      },
      fail: function (res) {

      },
      complete: function (res) {

      }
    });

  },
  initPrivacy() {
    wx.getPrivacySetting({
      success: res => {
        console.log(res) // 返回结果为: res = { needAuthorization: true/false, privacyContractName: '《xxx隐私保护指引》' }
        if (res.needAuthorization) {
          // 需要弹出隐私协议
          this.setData({
            showPrivacy: true
          })
        } else {
          // 用户已经同意过隐私协议，所以不需要再弹出隐私协议，也能调用已声明过的隐私接口
          // wx.getUserProfile()
          // wx.chooseMedia()
          // wx.getClipboardData()
          // wx.startRecord()
        }
      },
      fail: () => {},
      complete: () => {}
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})